﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Wpf_ClientLeger.ServiceConnexion;
using Wpf_ClientLeger.BusinessObject;
using Wpf_ClientLeger.IHM;

namespace Wpf_ClientLeger
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ServiceConnexionClient cli;
        Parametres param;

        public MainWindow()
        {
            cli = new ServiceConnexionClient();
            InitializeComponent();
        }

        //clic sur le bouton de validation du formulaire
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.label_erreur.Content = "";
           
            //Appel du webservice pour la connexion et l'instanciation de notre User
            try
            {
                User user = new User(cli.ConnectUser(this.textBoxLogin.Text, this.passwordBox1.Password));

                if (user.idUser != 0)
                {
                    //Connexion réussie !!
                    //result = MessageBox.Show("Bienvenue " + user.preUser + " " + user.nomUser);

                    //On instancie la nouvelle fenetre et on ferme l'écran de connexion
                    MenuPrincipal menu = new MenuPrincipal(param, user);
                    menu.Show();
                    this.Close();
                }
                else
                {
                    //this.textBoxLogin.Text = "";
                    //this.passwordBox1.Password = "";
                    this.label_erreur.Content = "ERREUR LOGIN OU PASSWORD";
                    //throw new Exception("Login ou mot de passe incorrect !!");
                }
            }
            catch (System.ServiceModel.EndpointNotFoundException ex)
            {
                this.label_erreur.Content = "ERREUR SERVEUR";
                Console.WriteLine(ex.Message);
                Console.Read();
            }
            catch (Exception e1)
            {
                this.label_erreur.Content = "ECHEC AUTHENTIFICATION";
            }
        }

        //au chargement de la fenêtre
        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            //Chargement de la table paramètres
            param = new Parametres(cli.ChargeParam());

            //focus sur le champ login
            FocusManager.SetFocusedElement(this, this.textBoxLogin);
        }

        /// <summary>
        /// Méthode permettant de valider si on appuie sur la touche "Entree"

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                button1_Click(sender, e);
            }
        }

    }
}
